<?php

namespace app\logic;

use app\ArrayToolkit;

class SendMessageLog extends BaseLogic
{
    public function createLog(array $fields)
    {
        $fields = $this->filterLogFields($fields);

        if (empty($fields)) {
            return null;
        }

        if (!ArrayToolkit::requireds($fields, ['template_id', 'user_id'])) {
            $this->throwHttpException(500, '缺少必要参数');
        }

        $id = $this->getSendMessageLogModel()->insertGetId($fields);
        return  $this->getSendMessageLogModel()->getById($id);
    }

    public function countSendMessageLog(array $conditions)
    {
        return $this->getSendMessageLogModel()->countSendMessageLog($conditions);
    }

    public function searchSendMessageLogs(array $conditions, array $orderBy, $start, $limit)
    {
        return $this->getSendMessageLogModel()->searchSendMessageLogs($conditions, $orderBy, $start, $limit);
    }

    protected function filterLogFields($fields): array
    {
        return ArrayToolkit::parts($fields, [
            'user_id',
            'template_id',
            'status',
            'created_time',
        ]);
    }

    /**
     * @return User
     */
    protected function getUserLogic(): User
    {
        return $this->getLogic('User');
    }

    /**
     * @return \app\model\SendMessageLog
     */
    protected function getSendMessageLogModel(): \app\model\SendMessageLog
    {
        return $this->getModel('SendMessageLog');
    }
}
